package com.jb.repair.acticity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.jb.repair.F1Application;
import com.jb.repair.R;
import com.jb.repair.adapter.CheckLocAdapter;
import com.jb.repair.dao.Db;
import com.jb.repair.dao.QueryDb;
import com.jb.repair.hardware.nfc.TagViewerActivity;
import com.jb.repair.model.PositionModel;
import com.jb.repair.util.MessageUtil;
import com.jb.repair.view.TitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * 点检位置
 * Created by gaobin on 2015/10/29.
 */
public class CheckLocActivity extends BaseActivity {
    private TextView tv_name;
    private ListView lv_data;
    private TitleView titleView;

    private List<PositionModel> checkLocModelList = new ArrayList<PositionModel>();
    private CheckLocAdapter checkLocAdapter;
    private SharedPreferences sp;
    public static String task_id;
    public static Boolean is_back = true;

    private final String TAG = "CheckLocActivity";

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                checkLocAdapter = new CheckLocAdapter(getApplicationContext(), checkLocModelList);
                lv_data.setAdapter(checkLocAdapter);
                MessageUtil.dismissProgressDialog();
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();

        // 修改页面状态，允许扫码
        TagViewerActivity.NOW_IN_PROPER_PAGE = true;

        if (is_back) {
            MessageUtil.showProgressDialog(CheckLocActivity.this, "正在加载");
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    checkLocModelList = QueryDb.getInstance(getApplicationContext()).getPositionList(task_id);
                    handler.sendEmptyMessage(0);
                }
            }.start();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        sp = getSharedPreferences("changable.txt", MODE_PRIVATE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_loc);
        is_back = true;
        tv_name = (TextView) this.findViewById(R.id.tv_name);
        tv_name.setText(F1Application.getUserName());
        lv_data = (ListView) this.findViewById(R.id.lv_data);
        titleView = (TitleView) this.findViewById(R.id.titleView);
        //  task_id = getIntent().getStringExtra("id");

        checkLocAdapter = new CheckLocAdapter(getApplicationContext(), checkLocModelList);
        lv_data.setAdapter(checkLocAdapter);

        titleView.setTitle("点检位置");
        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener() {
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                finish();
            }

            @Override
            public void onRightImage1Click(View view) {
                super.onRightImage1Click(view);
                Intent intent = new Intent(getApplicationContext(), CaptureActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        lv_data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                is_back = true;
                Intent intent = new Intent(getApplicationContext(), CheckRegisActivity.class);
                intent.putExtra("posId", checkLocModelList.get(position).id);
                intent.putExtra("posName", checkLocModelList.get(position).ins_position_name);
                intent.putExtra("jumpFromTag", false);//todo 测试用,记得改回false
                CheckRegisActivity.changable = sp.getBoolean("changable", false);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        is_back = false;
        if (resultCode == RESULT_OK) {
            String localId = data.getStringExtra("data");
            String local = "";
            String si_type = Db.getInstance(this).getName("si_type", "TB_SI_PLACE", "qrcode_id", localId);
            Log.i("Aaa","si_type为"+si_type);
            if (si_type != null && si_type.endsWith("0406901")) {
                local = Db.getInstance(this).getName("place_id", "TB_SI_PLACE", "qrcode_id", localId);
            }
            //根据二维码扫描返回的位置数据跳转到 CheckRegisActivity
            Intent intent = new Intent(getApplicationContext(), CheckRegisActivity.class);
            intent.putExtra("posId", local);
            intent.putExtra("jumpFromTag", true);
            CheckRegisActivity.changable = sp.getBoolean("changable", false);
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MessageUtil.dismissProgressDialog();
    }
}
